const Menu = require('../model/MenuModel')
const mongoose = require('mongoose')
const { ObjectId } = mongoose.Types
const { list2tree } = require('../utils/utils')

exports.list = async function (req, res, next) {
  const menus = await Menu.find()
  res.json({
    code: 20000,
    data: {
      items: menus
    },
    message: '菜单列表查询成功'
  })
}

exports.create = async function (req, res) {
  const data = req.body
  delete data.code
  if (!ObjectId.isValid(data.parent)) {
    delete data.parent
  };
  console.log('🚀 ~ file: menu.controller.js ~ line 18 ~ data', data)
  const menu = await Menu.create(data)
  res.json({
    code: 20000,
    message: '菜单创建成功',
    data: {
      menu
    }
  })
}

exports.createButton = async function (req, res, next) {
  const data = req.body

  const notButtonProperty = [
    'path',
    'component',
    'icon',
    'isHidden',
    'menuType'
  ]
  notButtonProperty.forEach((key) => {
    data[key] = null
  })
  if (!ObjectId.isValid(data.parent)) {
    delete data.parent
  }
  console.log('🚀 ~ file: menu.controller.js ~ line 18 ~ data', data)
  const button = await Menu.create(data)
  res.json({
    code: 20000,
    message: '按钮创建成功',
    data: {
      button
    }
  })
}

exports.getMenuTreeNotIncludeBtn = async function (req, res, next) {
  const menuTree = await Menu.find({ type: 'MENU' }).lean()
  const { treeData, objData } = list2tree(menuTree, null)
  res.json({
    code: 20000,
    message: '获取菜单树状结构(不包含button)成功',
    data: {
      items: treeData,
      total: treeData.length,
      objData
    }
  })
}

exports.getMenuTreeIncludeBtn = async function (req, res, next) {
  const menuTree = await Menu.find().lean()
  const { treeData, objData } = list2tree(menuTree, null)
  res.json({
    code: 20000,
    message: '获取菜单树状结构(包含button)成功',
    data: {
      items: treeData,
      total: treeData.length,
      objData
    }
  })
}

exports.updateMenu = async (req, res) => {
  const { _id, ...params } = req.body
  if (params.type === 'BUTTON') {
    const notButtonProperty = [
      'path',
      'component',
      'icon',
      'isHidden',
      'menuType'
    ]
    notButtonProperty.forEach((key) => {
      params[key] = null
    })
  } else {
    params.code = null
  }
  console.log('🚀 ~ file: menu.controller.js ~ line 77 ~ exports.updateMenu= ~ params', params)
  const result = await Menu.findByIdAndUpdate(_id, params)
  console.log('🚀 ~ file: menu.controller.js ~ line 78 ~ exports.updateMenu= ~ result', result)
  res.json({
    code: 20000,
    message: '菜单更新成功',
    data: result
  })
}

exports.remove = async (req, res) => {
  // 注意: 递归删除
  const id = req.params.id
  const ids = []
  await getChildrenIds(id, ids)
  console.log('🚀 ~ file: menu.controller.js ~ line 91 ~ exports.remove= ~ ids', ids)
  await Menu.deleteMany({ _id: { $in: ids } })
  res.json({
    code: 20000,
    message: '删除成功',
    data: ids
  })
}

async function getChildrenIds (parentId, ids) {
  ids.push(parentId)
  const result = await Menu.findOne({ _id: parentId }).populate('children').lean()
  while (result.children.length > 0) {
    const id = result.children.pop()._id
    getChildrenIds(id, ids)
  }
}
